package com.itheima.health.service.impl;


import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.Result;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import com.itheima.health.vo.HotSemeal;
import com.itheima.health.vo.ReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mfeng
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetMealDao setMealDao;


    /**
     * 运营数据统计⻚⾯展示
     * @return
     */
    @Override
    public ReportVO getBusinessReportData() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        ReportVO reportVO = new ReportVO();

        // 本周第一天
        LocalDate firstDayOfWeek = today.with(DayOfWeek.MONDAY);
        // 本周最后一天
        LocalDate lastDayOfWeek = today.with(DayOfWeek.SUNDAY);

        // 本月第一天
        LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        // 本月最后一天
        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());

        String reportDate = dateTimeFormatter.format(today);
        reportVO.setReportDate(reportDate);

        // 每日新增会员数
        Integer todayNewMember = memberDao.getMemberCount(today,today);
        reportVO.setTodayNewMember(todayNewMember);

        // 总会员数
        Integer totalMember = memberDao.getMemberTotalCount(today);
        reportVO.setTotalMember(totalMember);

        // 本周新会员数
        Integer thisWeekNewNumber = memberDao.getMemberCount(firstDayOfWeek, lastDayOfWeek);
        reportVO.setThisWeekNewMember(thisWeekNewNumber);

        // 本月新会员数
        Integer thisMonthNewMember = memberDao.getMemberCount(firstDayOfMonth, lastDayOfMonth);
        reportVO.setThisMonthNewMember(thisMonthNewMember);

        // 今日预约人数
        Integer todayOrderNumber = orderDao.getOrderCount(today,today,null);
        reportVO.setTodayOrderNumber(todayOrderNumber);

        // 本周预约人数
        Integer thisWeekOrderNumber = orderDao.getOrderCount(firstDayOfWeek, lastDayOfWeek,null);
        reportVO.setThisWeekOrderNumber(thisWeekOrderNumber);

        // 本月预约人数
        Integer thisMonthOrderNumber = orderDao.getOrderCount(firstDayOfMonth, lastDayOfMonth,null);
        reportVO.setThisMonthOrderNumber(thisMonthOrderNumber);

        // 今日到诊人数
        Integer todayVisitsNumber = orderDao.getOrderCount(today, today, Order.ORDERSTATUS_YES);
        reportVO.setTodayVisitsNumber(todayVisitsNumber);

        // 本周到诊人数
        Integer thisWeekVisitsNumber = orderDao.getOrderCount(firstDayOfWeek, lastDayOfWeek,Order.ORDERSTATUS_YES);
        reportVO.setThisWeekVisitsNumber(thisWeekVisitsNumber);

        // 本月到诊人数
        Integer thisMonthVisitNumber = orderDao.getOrderCount(firstDayOfMonth, lastDayOfMonth,Order.ORDERSTATUS_YES);
        reportVO.setThisMonthVisitsNumber(thisMonthVisitNumber);

        // 热门套餐
        List<HotSemeal> hotSemeals = orderDao.getHotSetmeal();
        reportVO.setHotSetmeal(hotSemeals);

        return reportVO;
    }


    /**
     * 查询套餐预约统计报告
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setMealDao.findSetmealCount();
    }
}